import 'package:flutter_test/flutter_test.dart';
import 'package:calculator/main.dart';

void main() {
  group('Calculator Basic Tests', () {
    testWidgets('Initial display shows 0', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      expect(find.text('0'), findsOneWidget);
    });

    testWidgets('Number input works correctly', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('1'));
      await tester.tap(find.text('2'));
      await tester.tap(find.text('3'));
      await tester.pump();
      expect(find.text('123'), findsOneWidget);
    });

    testWidgets('Basic addition works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('1'));
      await tester.tap(find.text('+'));
      await tester.tap(find.text('2'));
      await tester.tap(find.text('='));
      await tester.pump();
      expect(find.text('3'), findsOneWidget);
    });

    testWidgets('Basic subtraction works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('5'));
      await tester.tap(find.text('-'));
      await tester.tap(find.text('3'));
      await tester.tap(find.text('='));
      await tester.pump();
      expect(find.text('2'), findsOneWidget);
    });

    testWidgets('Basic multiplication works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('4'));
      await tester.tap(find.text('×'));
      await tester.tap(find.text('5'));
      await tester.tap(find.text('='));
      await tester.pump();
      expect(find.text('20'), findsOneWidget);
    });

    testWidgets('Basic division works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('8'));
      await tester.tap(find.text('÷'));
      await tester.tap(find.text('2'));
      await tester.tap(find.text('='));
      await tester.pump();
      expect(find.text('4'), findsOneWidget);
    });
  });

  group('Calculator Advanced Functions', () {
    testWidgets('Square root works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('9'));
      await tester.tap(find.text('√x'));
      await tester.pump();
      expect(find.text('3'), findsOneWidget);
    });

    testWidgets('Square works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('5'));
      await tester.tap(find.text('x²'));
      await tester.pump();
      expect(find.text('25'), findsOneWidget);
    });

    testWidgets('Reciprocal works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('2'));
      await tester.tap(find.text('1/x'));
      await tester.pump();
      expect(find.text('0.5'), findsOneWidget);
    });

    testWidgets('Percentage works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('50'));
      await tester.tap(find.text('%'));
      await tester.pump();
      expect(find.text('0.5'), findsOneWidget);
    });
  });

  group('Calculator Memory Functions', () {
    testWidgets('Memory store and recall works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('5'));
      await tester.tap(find.text('MS'));
      await tester.tap(find.text('C'));
      await tester.tap(find.text('MR'));
      await tester.pump();
      expect(find.text('5'), findsOneWidget);
    });

    testWidgets('Memory add works', (WidgetTester tester) async {
      await tester.pumpWidget(const CalculatorApp());
      await tester.tap(find.text('5'));
      await tester.tap(find.text('MS'));
      await tester.tap(find.text('3'));
      await tester.tap(find.text('M+'));
      await tester.tap(find.text('MR'));
      await tester.pump();
      expect(find.text('8'), findsOneWidget);
    });
  });

  testWidgets('Clear button resets calculator', (WidgetTester tester) async {
    await tester.pumpWidget(const CalculatorApp());
    await tester.tap(find.text('1'));
    await tester.tap(find.text('+'));
    await tester.tap(find.text('2'));
    await tester.tap(find.text('C'));
    await tester.pump();
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1 + 2'), findsNothing);
  });
}
